<?php

!defined('IN_ROOT') && exit('Access Denied');

class famousmodel {

    var $db;
    var $base;

    function famousmodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }

    function get_list($start=0,$limit=3) {
        $famouslist = array();
        $query = $this->db->query("SELECT * FROM ".DB_TABLEPRE."famous LIMIT $start ,$limit");
        while($famous = $this->db->fetch_array($query)) {
            $famous['avatar']=get_avatar_dir($famous['uid']);
            $famouslist[] = $famous;

        }
        return $famouslist;
    }

    function add($uids, $type=1) {
        $addfamoussql = "REPLACE INTO " . DB_TABLEPRE . "famous (`uid` ,`username` ,`qid` ,`title` ,`time` ) VALUES ";
        $query = $this->db->query("SELECT `uid`,`username`,`elect` FROM " . DB_TABLEPRE . "user WHERE uid IN ($uids) AND elect!=0");
        while ($user = $this->db->fetch_array($query)) {
            $answer = $this->get_solve_answer($user['uid']);
            $addfamoussql .= "(" . $user['uid'] . ",'" . $user['username'] ."'," . intval($answer['qid']) . ",'" . $answer['title'] . "'," . $this->base->time . "),";
        }
        return  $this->db->query(substr($addfamoussql, 0, -1));
    }

    function remove($uids) {
        return $this->db->query("DELETE FROM " . DB_TABLEPRE . "famous WHERE uid IN ($uids)");
    }

    function get_solve_answer($uid) {
        return $this->db->fetch_first("SELECT * FROM `".DB_TABLEPRE."answer` WHERE `authorid`=".$uid." AND `adopttime`>0 ORDER BY `adopttime` DESC");
    }

    function update_title($uid,$title,$qid) {
        $this->db->query("UPDATE ".DB_TABLEPRE."famous SET `title`='$title',qid=$qid WHERE uid=$uid");
    }
}

?>
